package com.aibaixun.web.controller.common;


import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.aibaixun.system.domain.PostureDevice;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>UploadFileUtil</b>
 *
 * @description: UploadFileUtil <br>
 * @date: 2024/5/27 09:21 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Component
public class UploadFileUtil {
    public List<PostureDevice> uploadFileList(MultipartFile file) {
        List<PostureDevice> postureDeviceList = new ArrayList<>();
        try {
            InputStream ins = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(ins);
            List<Map<String, Object>> maps = reader.readAll();
            // 从第三行开始读取
            AtomicInteger i = new AtomicInteger();
            maps.forEach(column -> {
                PostureDevice device = new PostureDevice();
                if (!String.valueOf(column.get("设备所属机柜唯一标识")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("设备所属机柜唯一标识")))) {
                    device.setCabinetUeid(String.valueOf(column.get("设备所属机柜唯一标识")));
                }
                if (!String.valueOf(column.get("设备所属机柜")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("设备所属机柜")))) {
                    device.setCabinetNum(String.valueOf(column.get("设备所属机柜")));
                }
                if (!String.valueOf(column.get("设备型号")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("设备型号")))) {
                    device.setModel(String.valueOf(column.get("设备型号")));
                }
                if (!String.valueOf(column.get("设备名称")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("设备名称")))) {
                    device.setName(String.valueOf(column.get("设备名称")));
                }
                if (!String.valueOf(column.get("起始")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("起始")))) {
                    device.setLocation(Integer.valueOf(String.valueOf(column.get("起始"))));
                }
                if (!String.valueOf(column.get("U高")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("U高")))) {
                    device.setHeight(Integer.valueOf(String.valueOf(column.get("U高"))));
                }
                postureDeviceList.add(device);
            });
        }catch (IOException e){

        }catch (Exception e){

        }
        return postureDeviceList;
    }
}
